package gui.custom;

import foxtrot.Task;
import foxtrot.Worker;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.Font;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.ImageIcon;

import actions.ToolBarAction;

public class ClickableLabel extends AALabel
{
	protected static final Color HIGHLIGHT = Color.CYAN;
	protected static final Color NORMAL = Color.WHITE;
	public ClickableLabel(String str)
	{
		super(str);
		init();
	}

	public ClickableLabel(String string, Font buttonfont)
	{
		super(string, buttonfont);
		init();
	}
	
	public ClickableLabel(ImageIcon icon)
	{
		super(icon);
		init();
	}
	
	public ClickableLabel()
	{
		super();
		init();
	}
	
	private void init()
	{
		setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));		
		
		super.addMouseListener(new MouseListener()
		{
			public void mouseEntered(MouseEvent e)
			{
				swapColour(true);
			}

			public void mouseExited(MouseEvent e)
			{
				swapColour(false);
			}

			public void mouseClicked(MouseEvent e){}
			public void mousePressed(MouseEvent e){}
			public void mouseReleased(MouseEvent e){}
		});
	}
	
	protected void swapColour(boolean highlight)
	{
		if (highlight)
		{
			setForeground(HIGHLIGHT);
		}
		else
		{
			setForeground(NORMAL);
		}
	}

	public void addMouseListener(final ToolBarAction action)
	{
		super.addMouseListener(new MouseListener()
		{
			public void mouseClicked(MouseEvent e)
			{
				if (e.getButton() == MouseEvent.BUTTON1)
				{
					action.actionPerformed(null);
				}
			}

			public void mouseEntered(MouseEvent arg0){}
			public void mouseExited(MouseEvent arg0){}
			public void mousePressed(MouseEvent arg0){}
			public void mouseReleased(MouseEvent arg0){}
		});
	}
	
	public void addMouseListener(final Task taskToRun)
	{
		super.addMouseListener(new MouseListener()
		{
			public void mouseClicked(MouseEvent arg0)
			{
				if (arg0.getButton() == MouseEvent.BUTTON1)
				{	
					try
					{
						Worker.post(taskToRun);
					}
					catch (Exception e)
					{
						e.printStackTrace();
					}						
				}		
			}
			public void mouseEntered(MouseEvent arg0){}
			public void mouseExited(MouseEvent arg0){}
			public void mousePressed(MouseEvent arg0){}
			public void mouseReleased(MouseEvent arg0){}			
		});
	}
	
}
